import { _decorator, Component, Label, Node, Sprite, SpriteAtlas } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { IDwGameEnd } from '../proto/gameZt';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames } from '../../Config/ResConfig';
import { DW_MzCfg, DW_WtCfg, DW_ZqCfg, DWTYPE } from '../../Config/GameCfg';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { GameDwEndZtGet_Ctr } from './GameDwEndZtGet_Ctr';
const { ccclass, property } = _decorator;

//主题赛段位结算
@ccclass('GameDwEndZt_Ctr')
export class GameDwEndZt_Ctr extends UIComponent {
    spIcon: Sprite;
    labReward: Label;
    labDwLv: Label;
    _click: boolean = true;
    atals: SpriteAtlas;
    dataCur: IDwGameEnd;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();

    }

    private InitUI(){
        this.registerEvent();

        this.Init();

        this.AddButtonListener('root/btn',this,this.onBtnGetClick);
    }

    private Init(){
        this.spIcon = this.ViewComponent('root/GameEnd_logo2/spIcon',Sprite);
        this.labReward = this.ViewComponent('root/Node/labNum',Label);
        this.labDwLv = this.ViewComponent('root/labLv',Label);

        this.atals = ResManager.inst.getAsset(AbNames.GamePlay,'Atals/GamePlay',SpriteAtlas) as SpriteAtlas;
    }

    private InitData(){
   
    }

    private registerEvent(){
        // globalThis.gameMgr._eventTarget.on(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private unRegisterEvent(){
        // globalThis.gameMgr._eventTarget.off(EventMsgKes.loading,this.onLoadingPro,this);
    }

    //段位奖励
    private onBtnGetClick(){
        //弹出球币奖励弹窗
        console.log('=========setCallBack========',this.dataCur);
        let com = UIMgr.inst.ShowUIView(AbNames.GamePlay,'Prefabs/GameZT/GameDwEndZtGet',UIType.Default) as GameDwEndZtGet_Ctr;
        com.setData(this.dataCur);
        //销毁当前节点
        this.node.destroy();
    }

    private showGetView(){
        
    }

    public setData(data: IDwGameEnd){

        this.dataCur = data;
        if(!this.spIcon){
            this.Init();
        }

        if(this.hasProInObj(data,'rewardNum')){
            this.labReward.string = `x${data.rewardNum}`;
        }

        if(this.hasProInObj(data,'dwType')){
            let dwType = data.dwType;
            let gameType = data.gameType;
            let str = "";
            if(this.hasProInObj(data,'gameType')){
                if(gameType === DWTYPE.DW_WENTI){
                    str = `${DW_WtCfg[dwType]}Lv${data.dwLv}`;
                }else if(gameType === DWTYPE.DW_ZUQIU){
                    str = `${DW_ZqCfg[dwType]}Lv${data.dwLv}`;
                }else if(gameType === DWTYPE.DW_MINGZU){
                    str = `${DW_MzCfg[dwType]}Lv${data.dwLv}`;
                }
            }
            this.labDwLv.string = str;

            let fileName = `GameEndTz/Icon/GameEndZt${gameType}_${dwType}`;
            let sf = this.atals.getSpriteFrame(fileName);
            console.log('=====GameDwEndZt_Ctr===setData========',sf);
            if(sf){
                this.spIcon.spriteFrame = sf;
            }
        }
    }
}


